package org.example.sort;

//希尔排序算法
public class ShellSort {
    public static void main(String[] args) {
        int[] arr = {10,15,97,15,64,75,0,23,78,62,197,2};
        //根据数组的长度确定增长量h的初始值
        int h=1;
        while (h<arr.length){
            h=2*h+1;
        }
        //排序算法
        while (h>=1) {
            for (int i = h; i < arr.length; i++) {
                for (int j = i; j >= h; j -= h) {
                    if (arr[j] < arr[j - h]) {
                        int temp = arr[j];
                        arr[j] = arr[j - h];
                        arr[j - h] = temp;
                    }else {
                        break;
                    }
                }
            }
            //更新h值
            h = h / 2;
        }
        //循环输出数组
        for (int i = 0; i<arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
    }
}
